package util

import (
	"errors"
	"unicode"
)

// CheckPassword check password complexicity
func CheckPassword(password string) error {
	if len(password) < 8 || len(password) > 24 {
		return errors.New("password's length should larger than 7 and less than 24")
	}

	var upperCasePresent bool
	var lowerCasePresent bool
	var numberPresent bool
	var specialCharPresent bool
	var containsBlank bool
	for _, ch := range password {
		switch {
		case unicode.IsNumber(ch):
			numberPresent = true
		case unicode.IsUpper(ch):
			upperCasePresent = true
		case unicode.IsLower(ch):
			lowerCasePresent = true
		case unicode.IsPunct(ch) || unicode.IsSymbol(ch):
			specialCharPresent = true
		case ch == ' ':
			containsBlank = true
		}
	}
	if upperCasePresent && lowerCasePresent && numberPresent && specialCharPresent && (!containsBlank) {
		return nil
	}
	return errors.New("password must contians number, upperCase, lowerCase and special char")
}
